<!DOCTYPE html>
<html lang="en">
<head>
    <title>角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css"/>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="searchBar">
    名称：
    <div class="layui-inline">
        <input class="layui-input" name="name" id="name" autocomplete="off" />
    </div>
    <button class="layui-btn" id="searchBtn" data-Type="reload">搜索</button>
</div>
<!--表格-->
<table id="role" lay-filter="role"></table>
<!--工具栏-->
<div id="toolbar" style="display: none">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</div>
<!--操作栏-->
<script type="text/html" id="operateBar">
    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除
    </a>
</script>
<div id="modal" style="display: none;">
    <form class="layui-form" lay-filter="entity">
        <input type="text" name="id" style="display: none;" />
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">角色标识</label>
            <div class="layui-input-inline">
                <input type="text" name="role" required lay-verify="required"
                       placeholder="请输入角色标识" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">显示顺序</label>
            <div class="layui-input-inline">
                <input type="text" name="roleSort" required lay-verify="required" placeholder="请输入显示顺序" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">显示状态</label>
            <div class="layui-input-inline">
                <input type="radio" name="status" value="0" title="正常"
                       checked=""><div class="layui-unselect layui-form-radio"><i class="layui-anim
                    layui-icon"> </i><div>正常</div></div>
                <input type="radio" name="status" value="1" title="停用"><div
                    class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim
                layui-icon"> </i><div>停用</div></div>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-inline">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限：</label>
            <div class="layui-input-inline">
                <div id="menuTree" name="menu" class="demo-tree"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <button class="layui-btn" lay-submit="" lay-filter="addandupdate">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary form-reset">重置
                </button>
            </div>
        </div>
    </form>
</div>
<script src="/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['table', 'layer', 'form', 'jquery','tree'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        var tree=layui.tree;
        $.get("/api/menu/getMenu", function (res){
            tree.render({
                elem: '#menuTree'
                ,id: 'treeReload'
                ,data: res.data
                ,showCheckbox: true  //是否显示复选框
            });
        });
        table.render({
            elem: '#role'
            , url: '/api/role/page'
            , page: true //开启分页
            , id: 'testReload'
            , cols: [[
                {field: 'id', title: 'ID', sort: true, fixed: 'left'}
                ,{field: 'role', title: '角色标识'}
                ,{field: 'roleSort', title: '显示顺序', sort: true}
                ,{field: 'remark', title: '备注'}
                ,{field: 'status', title: '权限标识', templet: function (d) {
                        return d.status === '0' ? '正常' : '停用';
                }}
                ,{field: 'createDatetime'
                    ,title: '创建时间'
                    ,sort: true
                    ,templet: "<div>{{layui.util.toDateString(d.createDatetime,'yyyy-MM-dd HH:mm:ss')}}</div>"}
                    ,{title: '操作', toolbar: '#operateBar', align:'center'
            }
            ]]
            , toolbar: '#toolbar',//表格头信息
            response: { // 设置响应码
                statusCode: 1 //规定成功的状态码，默认：0
            },
            parseData: function (rs) {//数据格式解析
                if (rs.status == 1) {
                    return {
                        "code": rs.status,
                        "msg": rs.message,
                        "count": rs.total,
                        "data": rs.data
                    }
                }
            }
        });
        table.on('toolbar(role)', function (obj) {
            switch (obj.event) {
                case 'add' :
                    tree.reload('treeReload', {})
                    open2('新增角色',null);
                    break;
            }
        });
        table.on('tool(role)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'delete':
                    layer.confirm("确定删除吗？", function (index) {
                        // 调用
                        $.ajax({
                            url : '/api/role/'+data.id,
                            type:'delete',
                            dataType:'json',
                            success: function (res) {
                                if(res.status == 1) {
                                    layer.msg(res.message);
                                    obj.del();
                                } else {
                                    layer.msg(res.message);
                                }
                            }
                        });
                        // 关闭对话框
                        layer.close(index);
                    });
                    break;
                case 'edit':
                    tree.reload('treeReload', {});
                    $.get('/api/roleMenu/getRoleMenu?roleId='+data.id, function (res) {
                        tree.setChecked('treeReload',res.data);
                    });
                    open2('修改角色',data);
                    break;
            }
        });
        function open2(title, entity){
            form.val('entity', entity);
            layer.open({
                type : 1
                ,title : title
                ,content : $('#modal')
                ,offset: '10px'
                ,area: ['500px', '95%']
                ,end : function () {
                    $('.form-reset').click();
                    table.reload('testReload')
                //active.reload();
                }
            });
        }
        form.on('submit(addandupdate)',function () {
            //取出表单中的数据项
            var data=form.val('entity');
            //构建数组,存放用户选中菜单ID
            var menuIds=new Array();
            //获取用户的选择项
            var checkedData=tree.getChecked('treeReload');
            var index=0;
            $.each(checkedData,function (i,obj) {
                menuIds[index++]=obj.id;
                $.each(obj.children,function (j,obj2) {
                    menuIds[index++]=obj2.id;
                })
            })
            data.menuIds=menuIds;
            var type = data.id ? 'put':'post';
            $.ajax({
                url : '/api/role',
                type : type,
                dataType : 'json',
                contentType : 'application/json',
                data: JSON.stringify(data),
                success: function (res) {
                    if (res.status == 1) {
                        layer.msg(res.message, {time:1*1000}, function () {
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
            return false;
        })
    });
</script>
</body>
</html>